import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.*;

/**
 * Minimal Device Discovery example.
 */
public class DeviceDiscovery {

    public static final Vector/*<RemoteDevice>*/ devicesDiscovered = new Vector();
    public static long tms_start = 0;
    public static void main(String[] args) throws IOException, InterruptedException {

    	long tms_diff;
        final Object inquiryCompletedEvent = new Object();

        devicesDiscovered.clear();

        DiscoveryListener listener = new DiscoveryListener() {
        	
            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
            	System.out.println("[discovered]" + System.currentTimeMillis()); //kjj
            	System.out.println("[time diff]" + (System.currentTimeMillis() - tms_start));
                
                devicesDiscovered.addElement(btDevice);
                System.out.println("##" + devicesDiscovered.size()); //kjj
                System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
                
                System.out.println("isTrusted: " + btDevice.isTrustedDevice());//kjj
                try {
                    System.out.println("Name " + btDevice.getFriendlyName(false));
                } catch (IOException cantGetDeviceName) {
                }
            }

            public void inquiryCompleted(int discType) {
                System.out.println("Device Inquiry completed!");
                synchronized(inquiryCompletedEvent){
                    inquiryCompletedEvent.notifyAll();
                }
            }

            public void serviceSearchCompleted(int transID, int respCode) {
            }

            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            }
        };

        synchronized(inquiryCompletedEvent) {
            boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            
            tms_start = System.currentTimeMillis();
            System.out.println("[query started]" + System.currentTimeMillis()); //kjj
            if (started) {
                System.out.println("wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
                System.out.println(devicesDiscovered.size() +  " device(s) found");
            }
        }
    }

}